//
//  RecordHeadViewController.m
//  iStore
//
//  Created by MacDev on 15/4/24.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "RecordHeadViewController.h"
#import "SendADViewController.h"

#define  kMaxLength  10

@interface RecordHeadViewController ()<UITextFieldDelegate>

@end

@implementation RecordHeadViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"制作冠名宣传内容";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextView)];

    _myTextField.delegate = self;
}

- (void)gotoNextView
{
    if (_myTextField.text.length == 0) {
        alert(@"请输入广告语");
        return;
    }
    SendADViewController * sVC = [[SendADViewController alloc]init];
    sVC.type = @"0";
    sVC.radioText = _myTextField.text;
    
    [self.navigationController pushViewController:sVC animated:YES];
}

-(IBAction)limitLength:(UITextField *)sender
{
    bool isChinese;//判断当前输入法是否是中文
    if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString: @"en-US"]) {
        isChinese = false;
    }
    else
    {
        isChinese = true;
    }
    
    if(sender == self.myTextField) {
        // 8位
        NSString *str = [[self.myTextField text] stringByReplacingOccurrencesOfString:@"?" withString:@""];
        if (isChinese) { //中文输入法下
            UITextRange *selectedRange = [self.myTextField markedTextRange];
            //获取高亮部分
            UITextPosition *position = [self.myTextField positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (!position) {

                _label.text = [NSString stringWithFormat:@"%lu/10字",(unsigned long)str.length];
                if ( str.length > kMaxLength) {
                    
                    _label.text = [NSString stringWithFormat:@"%d/10字",kMaxLength];
                    NSString *strNew = [NSString stringWithString:str];
                    [self.myTextField setText:[strNew substringToIndex:kMaxLength]];
                }
            }
            else
            {
//                NSLog(@"输入的英文还没有转化为汉字的状态");
                
            }
        }else{
 //           NSLog(@"str=%@; 本次长度=%lu",str,(unsigned long)[str length]);
            _label.text = [NSString stringWithFormat:@"%lu/10字",(unsigned long)str.length];
            if ([str length] > kMaxLength) {
                _label.text = [NSString stringWithFormat:@"%d/10字",kMaxLength];
                NSString *strNew = [NSString stringWithString:str];
                [self.myTextField setText:[strNew substringToIndex:kMaxLength]];
            }
        }
    }
}

- (IBAction)EditingChanged:(UITextField *)sender {
    [self.myTextField addTarget:self action:@selector(limitLength:) forControlEvents:UIControlEventEditingChanged];
}

@end
